﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Basic4Net.Entities;

namespace Basic4Net.BusinessRules
{
    public class AccountingRules : BaseBR
    {
        private static AccountingRules _accountingRulesInstance;
        public static AccountingRules AccountingRulestInstance
        {
            get { return _accountingRulesInstance ?? (_accountingRulesInstance = new AccountingRules()); }
        }
         

        /// <summary>
        /// 获取指定月的规则
        /// </summary>
        /// <param name="year">年份</param>
        /// <param name="month">月份</param>
        /// <param name="schoolMark">SchoolMark</param>
        /// <returns></returns>
        public IList<object> GetCurrentMonthRules(int year, int month, string schoolMark)
        {
            DBEntities db;
            using (db = new DBEntities())
            {
                var list = (from a in db.t_AccountingRules
                            join tt in db.t_TeacherType on a.ObjectRules equals tt.RID
                            into temp 
                            from t in temp
                            where a.FYear == year && a.FMonth == month && a.SchoolMark == schoolMark && t.FType != 1
                            select new { 
                                RID = a.RID,
                                RuleName = t.TypeName,
                                ActualWorkload = a.ActualWorkload,
                                Attendance = a.Attendance,
                                Safety = a.Safety,
                                DeductAtt = a.DeductAtt,
                                DeductSaf = a.DeductSaf,
                                SubsidyCls = a.SubsidyCls,
                                ObjectRules = a.ObjectRules
                            });
                return list.ToList<object>();
            }
        }

        /// <summary>
        /// 保存规则
        /// </summary>
        /// <param name="list">指定规则列表</param>
        /// <param name="msg">信息</param>
        /// <returns></returns>
        public bool SaveRules(IList<t_AccountingRules> list, ref string msg)
        { 
            DBEntities db;
            using (db = new DBEntities())
            {
                if (list == null || list.Count < 1)
                {
                    msg = "传入参数为空";
                    return false;
                }
                
                try
                {
                    foreach (t_AccountingRules t in list)
                    {
                        db.t_AccountingRules.FirstOrDefault(p => p.RID == t.RID);
                        db.ApplyCurrentValues("t_AccountingRules", t);
                    }
                    db.SaveChanges();
                }
                catch (Exception)
                {
                    msg = "保存失败";
                    return false;
                }
                return true;
            }
        }
    }
}
